<?php

/**
 * Created on 7 mai 2011
 *  * @author Samy Deghou(deghou@polytech.unice.fr ; deghou@embl.de)
 * 		   Stephan Beisken (beisken@ebi.ac.uk)
 * 		   Luis Felipe De Figueiredo (ldpf@ebi.ac.uk)
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
class Coupling implements iCRUD {
    /*
     * Attributes
     */

    private $_spectrumID;
    private $_atomID1;
    private $_atomID2;
    private $_constant;

    /*
     * Constructor
     */

    function __construct($_constant, $_spectrumID = -1, $_atomID1 = -1, $_atomID2 = -1) {

        $this->_spectrumID = $_spectrumID;
        $this->_atomID1 = $_atomID1;
        $this->_atomID2 = $_atomID2;
        $this->_constant = $_constant;
    }

    /*
     * Accessors
     */

    public function getAtomId1() {
        return $this->_atomID1;
    }

    public function getAtomId2() {
        return $this->_atomID2;
    }

    public function getspectrumID() {
        return $this->_spectrumID;
    }

    public function getConstant() {
        return $this->_constant;
    }

    /*
     * Modifiers
     */

    public function setAtomId1($param) {
        $this->_atomID1 = $param;
    }

    public function setAtomId2($param) {
        $this->_atomID2 = $param;
    }

    public function setspectrumID($param) {
        $this->_spectrumID = $param;
    }

    public function setConstant($param) {
        $this->_constant = $param;
    }

    /*
     * CRUD
     */

    /**
     * Put an object in the database
     */
    public function create() {
        Database::connect();
        if ($this->_spectrumID != -1 && $this->_atomID1 != -1 &&
                $this->_atomID2 != -1)
            die("Unable to create, still existing!");
        else {
            $query = "INSERT INTO `COUPLING` values ('" .
                    $this->_spectrumID . "','" .
                    $this->_atomID1 . "','" .
                    $this->_atomID2 . "','" .
                    $this->_constant . "')";
            mysql_query($query) or die(mysql_error());
            $this->_idSujet = mysql_insert_id();
            Database::disconnect();
        }
        return $this->_idSujet;
        Database::disconnect();
    }

    /**
     * Read an object from the database
     */
    public static function read($key) {
        Database::connect();
        $query = "SELECT * FROM COUPLING WHERE spectrum_id = '" . $key[0] .
                "' AND atom_id1 = '" . $key[1] .
                "' AND atom_id2 = '" . $key[2] . "'";
        $ans = mysql_fetch_assoc(mysql_query($query));
        extract($ans);
        return new Coupling($constant, $spectrum_id, $atom_id1, $atom_id2);
        Database::disconnect();
    }

    /**
     * Update an object from the database
     */
    public function update() {
        Database::connect();
        $query = "UPDATE `COUPLING` SET j_constant='" .
                $this->_constant . "'  WHERE spectrum_id='" .
                $this->_spectrumID . "' AND atom_id1='" .
                $this->_atomID1 . "' AND atom_id2='" .
                $this->_atomID2 . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect();
    }

    /**
     * Delete an object from the database
     */
    public function delete() {
        Database::connect();
        $query = "DELETE FROM `COUPLING` WHERE spectrum_id='" .
                $this->_spectrumID . "' AND atom_id1='" .
                $this->_atomID1 . "' AND atom_id2='" .
                $this->_atomID2 . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect();
    }

}
?>
